package com.linln.common.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Date;

/**
 * 助手函数(一些公共处理函数)
 * @author wangqun
 * @date 2025/01/13
 */

public class HelperUtil {

    /**
     * 根据身份证号获取年龄
     * @param idCard 身份证号码
     *
     */
    public static int getAge(String idCard) {
        if (idCard == null || idCard.length() != 18 || !idCard.matches("\\d{17}[0-9Xx]")) {
            return 0;
        }
        int year = Integer.parseInt(idCard.substring(6, 10));
        int month = Integer.parseInt(idCard.substring(10, 12));
        int day = Integer.parseInt(idCard.substring(12, 14));

        LocalDate birthDate = LocalDate.of(year, month, day);
        LocalDate currentDate = LocalDate.now();
        int age = Period.between(birthDate, currentDate).getYears();

        return age;
    }


    public static String dateFormat(Date expireDate, String format)  {
        if (expireDate == null || format == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(expireDate);
    }
}
